package com.example.demo.exception;

import java.util.Arrays;
import java.util.Collection;

import com.example.demo.typeEnums.APIResult;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

/**
 * API异常
 */
@Getter
@Setter
@Builder
public class APIException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    private int code;
    /**
     * 错误描述
     */
    private String msg;
    /**
     * 错误描述中占位符所对应的参数
     */
    private transient Object[] args;


    public APIException(APIResult result) {
        super(result.getMsg());
        this.code = result.getCode();
        this.msg = result.getMsg();
    }

    public APIException(APIResult result, Object... args) {
        super(result.getMsg());
        this.code = result.getCode();
        this.msg = result.getMsg();
        this.args = args;
    }

    public APIException(int code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public APIException(int code, String msg, Object... args) {
        super(msg);
        this.code = code;
        this.msg = msg;
        this.args = args;
    }

    public static <T> T requireNonNull(T obj, APIResult result) {
        if (obj == null) {
            throw new APIException(result);
        }
        return obj;
    }

    public static <T> Collection<T> requireNonEmpty(Collection<T> obj, APIResult result) {
        if (obj == null || obj.isEmpty()) {
            throw new APIException(result);
        }
        return obj;
    }

    /**
     * 如果指定对象为null，则抛出一个异常
     *
     * @param obj
     * @param result
     * @param args
     * @param <T>
     * @return
     */
    public static <T> T requireNonNull(T obj, APIResult result, Object... args) {
        if (obj == null) {
            throw APIException.builder()
                    .code(result.getCode())
                    .msg(result.getMsg())
                    .args(args)
                    .build();
        }
        return obj;
    }

    /**
     * 如果指定集合类型为空，则抛出一个API异常
     *
     * @param obj
     * @param result
     * @param args
     * @param <T>
     * @return
     */
    public static <T> Collection<T> requireNonEmpty(Collection<T> obj, APIResult result, Object... args) {
        if (obj == null || obj.isEmpty()) {
            throw APIException.builder()
                    .code(result.getCode())
                    .msg(result.getMsg())
                    .args(args)
                    .build();
        }
        return obj;
    }

    /**
     * 抛出一个API异常
     *
     * @param result
     * @param args
     */
    public static void throwAPIException(APIResult result, Object... args) {
        throw APIException.builder()
                .code(result.getCode())
                .msg(result.getMsg())
                .args(args)
                .build();
    }

    @Override
    public String toString() {
        return "APIException{"
                + "code=" + code
                + ", msg='" + msg + '\''
                + ", args=" + Arrays.toString(args) + '}';
    }
}
